package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.TradeWarPb;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;


@Cmd(code = Protocol.S_CROSS_TRADE_WAR_PUBLISH_WANTED_RESP, desc = "跨服商战发布通缉响应")
public class CrossTradeWarPublishWantedRespCmd extends AbstractCommand {
	private int activityId;
	private TradeWarWanted tradeWarWanted;
	private int useItemId;
	private boolean isAddRecord;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg reqMsg = CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		ret = reqMsg.getRet();
		useItemId = reqMsg.getUseItemId();
		if(0 == ret) {
			isAddRecord = reqMsg.getIsAddWantedRecord();
			tradeWarWanted = TradeWarPb.parseTradeWarWanted(reqMsg.getWantedInfo());
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(0 == useItemId){
			useItemId = GameConfig.TRADE_WAR_WANTED_ITEM_ID;
		}
		Property consume = new Property(useItemId, BigInteger.ONE);
		//恢复预扣除
		player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parsePropertyToString(consume));
		if(0 == ret){
			//扣除道具
			player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.TradeWar, eLogMoneyType.CrossTradeWarPublishWanted);
			ret = player.getModule(TradeWarModule.class).publishCrossTradeWarWantedResponse(activityId, tradeWarWanted, consume, isAddRecord);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
        CrossTradeWarProto.CrossTradeWarPublishWantedRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarPublishWantedRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_CROSS_TRADE_WAR_PUBLISH_WANTED, respMsg);
	}

}
